Enemy = Class.extend ({

	init: function(context, imagePath) {
		this.context = context;
		this.img = imagePath;
		
		this.counter = 0;
		this.numberOfFrame = 5;
		this.frameIndex = 0;
		this.scale = 1;
		this.curentPositionEnemy = [] ;
		this.intervalWalk = null;
		this.shootFromEnemy = false;
	},
	
	drawWalkEnemy: function() {
		var self = this;
		var frame = self.frameIndex;
		this.clear(self.curentPositionEnemy[0], self.curentPositionEnemy[1]);
		this.context.save();
		this.context.scale(self.scale, self.scale);
		this.context.drawImage(self.img, ( frame * 100), 0,
								100, 154,
								self.curentPositionEnemy[0], self.curentPositionEnemy[1],
								100, 154);
		this.context.restore();
	},
	
	drawShooterEnemy: function() {
		var self = this;
		this.clear(self.curentPositionEnemy[0], self.curentPositionEnemy[1]);
		this.context.save();
		this.context.scale(self.scale, self.scale);
		this.context.drawImage(self.img, 500 , 0,
								100, 154,
								self.curentPositionEnemy[0], self.curentPositionEnemy[1],
								100, 154);
		this.context.restore();
	},
	
	drawDeadEnemy: function() {;
		var self = this;	
		var frame = self.frameIndex;
		this.clear(self.curentPositionEnemy[0], self.curentPositionEnemy[1]);
		this.context.save();
		this.context.scale(self.scale, self.scale);
		this.context.drawImage(self.img, ( frame * 100) , 154,
								100, 154,
								self.curentPositionEnemy[0], self.curentPositionEnemy[1],
								100, 154);
		this.context.restore();
	},
	
	walkEnemy: function() {
		var self = this;
		self.intervalWalk = setInterval(function() {
			self.drawWalkEnemy();
			self.curentPositionEnemy[0] += 4;
			self.curentPositionEnemy[1] += 4;
			self.frameIndex ++;
	        if (self.frameIndex >= self.numberOfFrame-1) {	
	        	self.drawWalkEnemy();
	        	clearInterval(self.intervalWalk);	
	        } 
		}, 1000/9);
	},
	
	dyingEnemy: function(shoot) {
		this.shoot = shoot;
		var self = this;
		
		if(this.shoot === 1){
			self.frameIndex = 0;
			self.drawDeadEnemy();
			self.intervalDying = setInterval(function() {
				
				if(self.frameIndex < self.numberOfFrame){
					self.frameIndex++;
					self.drawDeadEnemy();
					
				} else {
					clearInterval(self.intervalDying);
					clearInterval(self.intervalWalk);
					self.clear((self.curentPositionEnemy[0]), (self.curentPositionEnemy[1]));
				}
			}, 1000/8);
		}
		
	},
	
	clear: function(x, y) {
		this.context.save();
		this.context.scale(this.scale, this.scale);
		this.context.clearRect((x-10), (y-10), 130, 190);
		this.context.restore();
	},
	
	initEnemy: function(enemyAlive) {
		this.walkEnemy(enemyAlive);
		
		return [(this.scale * this.curentPositionEnemy[0] - 10), (this.scale * this.curentPositionEnemy[1] - 10), 
		        (this.scale *(this.curentPositionEnemy[0] + 100 - 50)), (this.scale *(this.curentPositionEnemy[1] +  154 - 40))];
	}
	
});



